package com.linxz.ui.spraycarview;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;

import com.linxz.ui.R;

import java.util.ArrayList;
import java.util.List;


public class SprayItemAdapter extends RecyclerView.Adapter<SprayItemViewHolder> {
    private List<SprayItem2> mSprayItem2s=new ArrayList<>();
    private SprayItemData data;
    @NonNull
    @Override
    public SprayItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SprayItemViewHolder(parent);
    }

    @Override
    public void onBindViewHolder(@NonNull SprayItemViewHolder holder, int position) {
           if(position>=0&&position<mSprayItem2s.size()){
               final SprayItem2 item2=mSprayItem2s.get(position);
               if(item2!=null){
                  String item3Name="";
                  if(item2.getSelectedId()!=null&&item2.getList()!=null){
                      for(SprayItem3 item3:item2.getList()){
                          if(item2.getSelectedId().equals(item3.getId())){
                              item3Name=item3.getTitle();
                          }
                      }
                  }
                  String name=String.format("%s (%s)",item2.getTitle(),item3Name);
                  holder.tvItemName.setText(name);
                  holder.ivDelete.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          int position=mSprayItem2s.indexOf(item2);
                          if(position!=-1){
                              item2.setSelectedId(null);
                              if(data!=null){
                                  data.refresh();
                              }
                          }
                      }
                  });
               }
           }

    }
    @Override
    public int getItemCount() {
        return mSprayItem2s.size();
    }

    public void bindData(LifecycleOwner owner, SprayItemData data){
        this.data=data;
        this.data.observe(owner, new Observer<List<SprayItem1>>() {
            @Override
            public void onChanged(@Nullable List<SprayItem1> sprayItem1s) {
                  mSprayItem2s.clear();
                  if(sprayItem1s!=null){
                      for(SprayItem1 item1:sprayItem1s){
                          for(SprayItem2 item2:item1.getList()){
                              if(item2.getSelectedId()!=null){
                                  mSprayItem2s.add(item2);
                              }
                          }
                      }
                  }
                  notifyDataSetChanged();
            }
        });
    }

    public void clear(){
        if(mSprayItem2s!=null){
            for (SprayItem2 item2:mSprayItem2s){
                 item2.setSelectedId(null);
            }
            mSprayItem2s.clear();
        }
        if(data!=null){
            data.refresh();
        }
    }

    public String getItemIds(){
           StringBuffer buffer=new StringBuffer();
           List<SprayItem2> items=new ArrayList<>(mSprayItem2s);
           for(int i=0;i<items.size();i++){
               SprayItem2 item=items.get(i);
               String selectId=item.getSelectedId();
               if(!TextUtils.isEmpty(selectId)){
                   buffer.append(selectId);
                   if(i!=items.size()-1){
                       buffer.append(",");
                   }
               }
           }
           return  buffer.toString();
    }
}

class SprayItemViewHolder extends RecyclerView.ViewHolder{
    public ImageView ivDelete;
    public TextView  tvItemName;
    public SprayItemViewHolder(ViewGroup parent) {
        super(LayoutInflater.from(parent.getContext()).inflate(R.layout.spray_recycler_item_sprayitem,parent,false));
        ivDelete=itemView.findViewById(R.id.ivDelete);
        tvItemName=itemView.findViewById(R.id.tvItemName);
    }
}
